package demo.common.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 通用异常处理类
 */
@ControllerAdvice //标准当前类是通用异常处理类
public class BasicExceptionAdvice {

    /**
     * 运行期异常处理方案
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> handleRuntimeException(RuntimeException e){
        return ResponseEntity.status(500).body(e.getMessage());
    }

    /**
     * 自定义异常处理方案
     */
    @ExceptionHandler(SnowException.class)
    public ResponseEntity<ExceptionResult> handleLyException(SnowException e){
        return ResponseEntity.status(e.getStatus()).body(new ExceptionResult(e));
    }
}
